﻿using System;
using System.Collections.Generic;
using Microsoft.Practices.ServiceLocation;
using MyFramework.Common.Interfaces;

namespace MyFramework.Helpers
{
    /// <summary>
    /// Controller Helper
    /// </summary>
    public class ControllerHelpers
    {
        /// <summary>
        /// Creates the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="onInitializedAction">The configuration initialized action.</param>
        /// <returns></returns>
        public IController Create(Type type, Action<IController> onInitializedAction)
        {
            return this.Create(type, null, null, onInitializedAction, null);
        }

        /// <summary>
        /// Creates the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="initParameters">The initialize parameters.</param>
        /// <param name="onInitializedAction">The configuration initialized action.</param>
        /// <returns></returns>
        public IController Create(Type type, IDictionary<string, object> initParameters, Action<IController> onInitializedAction)
        {
            return this.Create(type, initParameters, null, onInitializedAction, null);
        }

        /// <summary>
        /// Creates the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="initParameters">The initialize parameters.</param>
        /// <param name="onBeforeAdd">The configuration before add.</param>
        /// <param name="onInitializedAction">The configuration initialized action.</param>
        /// <returns></returns>
        public IController Create(Type type, IDictionary<string, object> initParameters, Action<IController> onBeforeAdd, Action<IController> onInitializedAction)
        {
            return this.Create(type, initParameters, onBeforeAdd, onInitializedAction, null);
        }

        /// <summary>
        /// Creates the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="initParameters">The initialize parameters.</param>
        /// <param name="onBeforeAdd">The configuration before add.</param>
        /// <param name="onInitializedAction">The configuration initialized action.</param>
        /// <param name="onException">The configuration exception.</param>
        /// <returns></returns>
        public IController Create(Type type, IDictionary<string, object> initParameters, Action<IController> onBeforeAdd, Action<IController> onInitializedAction, Action<Exception> onException)
        {
            IController current = null;
            ServiceLocator.Current.GetInstance<IControllerFactory>().CreateController(
                type,
                initParameters,

                c =>
                {
                    current = c;
                    if (onBeforeAdd != null)
                    {
                        onBeforeAdd(c);
                    }
                },
                c =>
                {
                    if (onInitializedAction != null)
                    {
                        onInitializedAction(c);
                    }
                },
                onException);

            return current;
        }

        /// <summary>
        /// Creates the asynchronous.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="onInitializedAction">The configuration initialized action.</param>
        /// <returns></returns>
        public IController CreateSync(Type type, Action<IController> onInitializedAction)
        {
            return this.CreateSync(type, null, null, onInitializedAction, null);
        }

        /// <summary>
        /// Creates the asynchronous.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="initParameters">The initialize parameters.</param>
        /// <param name="onInitializedAction">The configuration initialized action.</param>
        /// <returns></returns>
        public IController CreateSync(Type type, IDictionary<string, object> initParameters, Action<IController> onInitializedAction)
        {
            return this.CreateSync(type, initParameters, null, onInitializedAction, null);
        }

        /// <summary>
        /// Creates the asynchronous.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="initParameters">The initialize parameters.</param>
        /// <param name="onBeforeAdd">The configuration before add.</param>
        /// <param name="onInitializedAction">The configuration initialized action.</param>
        /// <returns></returns>
        public IController CreateSync(Type type, IDictionary<string, object> initParameters, Action<IController> onBeforeAdd, Action<IController> onInitializedAction)
        {
            return this.CreateSync(type, initParameters, onBeforeAdd, onInitializedAction, null);
        }

        /// <summary>
        /// Creates the asynchronous.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="initParameters">The initialize parameters.</param>
        /// <param name="onBeforeAdd">The configuration before add.</param>
        /// <param name="onInitializedAction">The configuration initialized action.</param>
        /// <param name="onException">The configuration exception.</param>
        /// <returns></returns>
        public IController CreateSync(Type type, IDictionary<string, object> initParameters, Action<IController> onBeforeAdd, Action<IController> onInitializedAction, Action<Exception> onException)
        {
            IController current = null;
            ServiceLocator.Current.GetInstance<IControllerFactory>().CreateControllerSync(
                type,
                initParameters,
                c =>
                {
                    current = c;
                    if (onBeforeAdd != null)
                    {
                        onBeforeAdd(c);
                    }
                },
                c =>
                {
                    if (onInitializedAction != null)
                    {
                        onInitializedAction(c);
                    }
                },
                onException);

            return current;
        }

    }
}
